<?php

include(dirname(__FILE__) . "/../../../include/global.php");
include(dirname(__FILE__) . "/../model/nms_script_model.php");
include(dirname(__FILE__) . "/../model/nms_alarm_model.php");
include(dirname(__FILE__) . "/../libs/sms.php");
include(dirname(__FILE__) . "/../libs/config.php");
include(dirname(__FILE__) . "/../model/nms_setting_model.php");
//get host
$host = db_fetch_assoc('select id, hostname,description, snmp_community
							from host a, plugin_nms_interface b
							where a.id= b.host_id and a.status != 1 group by id');
$count =0;
if(!empty($host)){
	foreach($host as $item){
		$inter = db_fetch_assoc('select interface_id, snmp_index, nms_status, status_oid
							from plugin_nms_interface
							where host_id  = '.$item['id'].' and present =1');
		foreach($inter as $item1){
			// get status
			$status = snmp2_get($item['hostname'],$item['snmp_community'],$item1['status_oid']);
			$tem = substr($status, -2,1);
			if($tem == 1){
				$status = "Up";
			}else{
				if($tem == 2){
					$status = "Down";
				}else{
					$status = "Unknown";
				}
			}
			if($status != $item1['nms_status']){
				$count++;
				$logsms;
				$group_interface = db_fetch_cell('SELECT interface_group_id from plugin_nms_interfacegroup where interface_id ='.$item1['interface_id'].'');
				if($group_interface==''){
					$group_interface =1;
					add_one_interface_to_groupdefault($item1['interface_id']);
				}
				script_update_status_interface($item1['interface_id'],$status);
				date_default_timezone_set('Asia/Ho_Chi_Minh');
				$today=date("d/m/Y-H:i:s", time());
				$numlist = array('0906393929','0914186188');
				$namein = db_fetch_cell('SELECT interface_name FROM plugin_nms_interface WHERE interface_id = '.$item1['interface_id'].'');
				$alias;
				//send sms
				if($status == "Up"){
					// auto unpause
					script_update_resume_interface($item1['interface_id']);
					// refresh interface alias
					$alias = db_fetch_cell('SELECT alias_oid FROM plugin_nms_interface where interface_id = '.$item1['interface_id'].'');
					// get alias
					$alias = snmp2_get($item['hostname'],$item['snmp_community'],$alias);

					$alias = str_replace('STRING: ', '', $alias);
					$alias = trim($alias, '" "');
					// update alias into nms, plugin_nms_interface
					update_alias_interface($alias, $item1['interface_id']);

					//update status into cacti, host_snmp_cache
					update_field_value_host_snmp_cache($status,$item['id'],$item1['status_oid']);

					//update alias into cacti, host_snmp_cache
					update_field_value_host_snmp_cache($alias,$item['id'],$item1['alias_oid']);
					if($item1['nms_status'] != "Unknown"){
					$group_phone = db_fetch_assoc('SELECT contact_group_id FROM plugin_nms_contactdetail WHERE interface_group_id = '.$group_interface.'');	
						foreach ($group_phone as $get){
							$phone =getContactlist($get['contact_group_id']);
							foreach ($phone as $get1){
								$res = sendsms($url_webservice,$user_name_webservice,$password_webservice,$get1['sodt'],'('.$item1['interface_id'].') '.$today.' - '.$item['description'].' ('.$namein.') '.$alias.' Change state to Up.'.$note_sms);
								if($res->SmsMTResult != 'OK'){
									$logsms = $logsms.' '.$get1['sodt'];
								}							
							}
						}
					}
					//for($i=0;$i<count($numlist);$i++){
						//$res = sendsms('http://10.70.28.200:8080/service1.asmx?WSDL', 'ws_andaibr', 'andaibr',$numlist[$i], ''.$today.' - '.$item['description'].' ('.$namein.') '.$alias.' Change step: Down -> Up');
					//}
						cacti_log('Network Monitoring System (Poller): Sent sms to '.count($numlist).' persons', false);	
				}
				if($status == "Down" and $item1['nms_status'] != "Unknown"){
					//get alias
					$alias = db_fetch_cell('SELECT interface_alias FROM plugin_nms_interface where interface_id = '.$item1['interface_id'].'');
					$group_phone = db_fetch_assoc('SELECT contact_group_id FROM plugin_nms_contactdetail WHERE interface_group_id = '.$group_interface.'');
					foreach ($group_phone as $get){
						$phone =getContactlist($get['contact_group_id']);
						foreach ($phone as $get1){
							$res = sendsms($url_webservice, $user_name_webservice, $password_webservice,$get1['sodt'], '('.$item1['interface_id'].') '.$today.' - '.$item['description'].' ('.$namein.') '.$alias.' Change state to Down.'.$note_sms);
							if($res->SmsMTResult != 'OK'){
								$logsms = $logsms.' '.$get1['sodt'];
							}
						}
					}
					//for($i=0;$i<count($numlist);$i++){
						//$res = sendsms('http://10.70.28.200:8080/service1.asmx?WSDL', 'ws_andaibr', 'andaibr',$numlist[$i], ''.$today.' - '.$item['description'].' ('.$namein.') '.$alias.' Change step: Up -> Down');
					//}
					cacti_log('Network Monitoring System (Poller): Sent sms to '.count($numlist).' person', false);					
				}
				if($logsms == ""){
					if($status == "Unknown"){
						$logsms ="Not send";
					}
					else{
						$logsms = "Sent";
					}
				}
				if($status != "Unknown"){
					addProblem($item1['interface_id'],$item['description'],'('.$namein.') '.$alias,$status);
				}
				script_add_log_into_table($item1['interface_id'],$status,$logsms);
			}
		}

	}
	cacti_log('Network Monitoring System (Poller): '.$count.' interfaces have changed', false);
}
else {
	cacti_log('Network Monitoring System (Poller): interface does not exist ', false);
}
// poller host
$currenthost = db_fetch_assoc('Select * From(Select host_id,host_status from plugin_nms_hostlog order by host_time DESC) as a group by host_id');
$host_phone = db_fetch_assoc('Select contact_group_id From plugin_nms_contactdetail Where interface_group_id=2');
foreach ($currenthost as $item){
	$oldhost = db_fetch_row('Select status,description from host where id = '.$item['host_id'].'');
	if($oldhost['status'] != $item['host_status']){
		add_one_hostlog($item['host_id']);
		if($oldhost['status'] ==1){
			addsoundevice($item['host_id']);
			foreach ($host_phone as $set){
				$phone =getContactlist($set['contact_group_id']);
				foreach ($phone as $set1){
					$today=date("d/m/Y-H:i:s", time());
					$res = sendsms($url_webservice,$user_name_webservice,$password_webservice,$set1['sodt'],' Warning: '.$today.' Host :'.$oldhost['description'].' change state to Down.'.$note_sms);
				}
			}
		}
		if($oldhost['status'] == 3 and $item['host_status' == 1]){
			foreach ($host_phone as $set){
				$phone =getContactlist($set['contact_group_id']);
				foreach ($phone as $set1){
					$today=date("d/m/Y-H:i:s", time());
					$res = sendsms($url_webservice,$user_name_webservice,$password_webservice,$set1['sodt'],' Warning: '.$today.' Host :'.$oldhost['description'].' change state to Up.'.$note_sms);
				}
			}
		}
	}
}